package pers.mihao.quickstream.common.util.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

public class SpringAllAwareRegister implements ApplicationContextInitializer {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        applicationContext.addBeanFactoryPostProcessor(new SpringAllAwareRegisterPostProcessor());
    }

    static class SpringAllAwareRegisterPostProcessor implements BeanDefinitionRegistryPostProcessor {

        @Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
            registry.registerBeanDefinition("springAllAware", new RootBeanDefinition(SpringAllAware.class));
        }

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        }
    }


    public static class SpringAllAware implements ApplicationContextAware, BeanClassLoaderAware
            , BeanFactoryAware, EnvironmentAware, ResourceLoaderAware {

        private ClassLoader classLoader;
        private BeanFactory beanFactory;
        private ApplicationContext applicationContext;
        private Environment environment;
        private ResourceLoader resourceLoader;


        @Override
        public void setBeanClassLoader(ClassLoader classLoader) {
            this.classLoader = classLoader;
        }

        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            this.beanFactory = beanFactory;
        }

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }

        @Override
        public void setEnvironment(Environment environment) {
            this.environment = environment;
        }

        @Override
        public void setResourceLoader(ResourceLoader resourceLoader) {
            this.resourceLoader = resourceLoader;
        }

        public ClassLoader getClassLoader() {
            return classLoader;
        }

        public void setClassLoader(ClassLoader classLoader) {
            this.classLoader = classLoader;
        }

        public BeanFactory getBeanFactory() {
            return beanFactory;
        }

        public ApplicationContext getApplicationContext() {
            return applicationContext;
        }

        public Environment getEnvironment() {
            return environment;
        }

        public ResourceLoader getResourceLoader() {
            return resourceLoader;
        }
    }


}

